package com.zjitc.controller;

import com.zjitc.beans.MessageBean;
import com.zjitc.pojo.User;
import com.zjitc.service.imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserServiceImpl service;


    @RequestMapping("/role")
    public ModelAndView role(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if(!checkAdmin(session)){
            modelAndView.setViewName("login");
        }else{
            List<User> users = service.getUserInfoList();
            Integer count = service.getUserCount();
            modelAndView.addObject("users", users);
            modelAndView.addObject("count",count);
            modelAndView.setViewName("user/role");
        }
        return modelAndView;
    }

    @RequestMapping("/toEdit")
    public ModelAndView toEdit(String uid,HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if(!checkAdmin(session)){
            modelAndView.setViewName("login");
        }else{
            User user = service.findbyuid(uid);
            modelAndView.addObject("editUser", user);
            modelAndView.setViewName("user/editRole");
        }
        return modelAndView;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(User old) {
        User newUser = service.findbyuid(old.getUid());
        newUser.setUsername(old.getUsername());
        newUser.setIntegral(old.getIntegral());
        service.update(newUser);
        return new MessageBean(true, "修改成功");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(String uid, HttpSession session) {
        if(getSessionAdmin(session) != null){
            boolean deluser = service.deluser(uid);
            if(deluser) {
                return new MessageBean(true, "删除成功");
            }
        }
        return new MessageBean(false,"删除失败");
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public Object updateState(String username ,HttpSession session){
        if (!checkAdmin(session)){
            return "-1";
        }else{
            return service.updateState(username);
        }
    }

    @RequestMapping("/toAddRole")
    public String toAddRole() {
        return "user/addRole";
    }

//    @RequestMapping("/addRole")
//    @ResponseBody
//    public Object addRole(User old) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//        boolean flag = userService.register(old);
//        return new MessageBean(flag, flag ? "添加用户成功" : "添加用户失败");
//    }

    @RequestMapping("/findUser")
    public ModelAndView findUser(String username,HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if(getSessionAdmin(session) ==null){
            modelAndView.setViewName("login");
        }else{
            List<User> user = service.findUserSearch(username);
            modelAndView.addObject("users", user);
            modelAndView.setViewName("user/role");
        }
        return modelAndView;
    }
//
//    @RequestMapping("/deleteAllUser")
//    @ResponseBody
//    public Object deleteAllUser() {
//    userService.deleteAllUser();
//        return new MessageBean(true, "删除成功");
//    }
//
    @ResponseBody
    @RequestMapping("/deletes")
    public Object deletes(String ids, HttpServletRequest request) {
        boolean flag = true;
        String[] split = ids.split(",");
        for (String uid : split) {
            boolean del = service.deluser(uid);
            if(del == false){
                flag = false;
            }
        }
        return new MessageBean(flag, "删除成功");
    }

    @RequestMapping("/test")
    public String test(){
        return "echart";
    }
}
